package com.frxuexi.user.view

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import com.frxuexi.lib_base.MyTitleBar
import com.frxuexi.lib_base.RouterPath
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.AskLeave
import com.frxuexi.user.R
import com.frxuexi.user.intent.AskIntent
import com.frxuexi.user.viewmodel.AskViewModel
import com.google.gson.Gson
import kotlinx.coroutines.launch

@Composable
fun ApproveListPage(navHostController: NavHostController,vm: AskViewModel = hiltViewModel()) {
    var list = remember {
        SnapshotStateList<AskLeave>()
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        list.clear()
                        list.addAll(it.data as Collection<AskLeave>)
                    }

                    else -> {}
                }
            }
        }

        vm.sendIntent(AskIntent.getNoApprove)
    })

    Column {
        MyTitleBar(title = "待审核", navHostController = navHostController)
        Row(modifier = Modifier
            .padding(horizontal = 20.dp)
            .background(colorResource(id = R.color.lightblue))
            .padding(horizontal = 20.dp, vertical = 15.dp)
            .fillMaxWidth(),horizontalArrangement = Arrangement.SpaceAround) {
            Text(text = "姓名")
            Text(text = "类型")
            Text(text = "开始时间")
            Text(text = "结束时间")

        }
        Spacer(modifier = Modifier.height(10.dp))
        LazyColumn(modifier = Modifier
            .padding(horizontal = 20.dp)
            .fillMaxWidth(), verticalArrangement = Arrangement.spacedBy(10.dp), content = {
            items(list){
                ElevatedCard(modifier = Modifier.clickable {
                    val json = Gson().toJson(it)
                    navHostController.navigate(RouterPath.APPROVEDETAIL_PAGE+"/$json")
                }) {
                    Row(verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.SpaceAround,modifier = Modifier
                        .padding(horizontal = 20.dp, vertical = 15.dp)
                        .fillMaxWidth()) {
                        Text(text = it.uname!!)
                        Text(text = it.type!!)
                        Text(text = it.start!!)
                        Text(text = it.end!!)
                    }
                }

            }
        })
    }
}